import {getMerchantByDomainName} from "../../api/system";
import storage from 'store'
import {MERCHANT_ID} from "../types";

const merchants = {
  state: {
    merchantInfo: ''
  },

  mutations: {
    SET_MERCHANT_INFO: (state, merchantInfo) => {
      state.merchantInfo = merchantInfo
    }
  },
  actions: {

    GetMerchantInfo({ commit }, domainName) {
      return new Promise((resolve, reject) => {
        getMerchantByDomainName({domainName: domainName}).then(result => {
          const sysMerchant = result.data.sysMerchant;
          if (sysMerchant) {
            storage.set(MERCHANT_ID, sysMerchant.id, 7 * 24 * 60 * 60 * 1000)
            commit('SET_MERCHANT_INFO', sysMerchant)
            resolve()
          } else {
            reject(new Error('获取商户失败'))
          }
        })
      })
    },

    ClearMerchantInfo({ commit }) {
      return new Promise((resolve) => {
        storage.remove(MERCHANT_ID)
        commit('SET_MERCHANT_INFO', '')
        resolve()
      })
    },
  }
}

export default merchants
